package com.jake.xfeed.recyclerfeed.pager;

import android.util.Log;
import android.util.SparseArray;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jake.xfeed.recyclerfeed.BasePagerCard;
import com.jake.xfeed.recyclerfeed.FeedViewHolder;
import com.jake.xfeed.recyclerfeed.IDataSource;
import com.jake.xfeed.recyclerfeed.RecyclerViewFeedAdapter;

import java.lang.ref.WeakReference;

public class RecyclerViewPagerAdapter<Data> extends RecyclerViewFeedAdapter<Data> {

    protected final SparseArray<WeakReference<FeedViewHolder>> mAttachedViewHolders = new SparseArray<>();

    public RecyclerViewPagerAdapter(IDataSource<Data> dataSource) {
        super(dataSource);
    }

    @NonNull
    @Override
    public FeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FeedViewHolder holder = super.onCreateViewHolder(parent, viewType);
        holder.itemView.setLayoutParams(generateDefaultLayoutParams());
        return holder;
    }

    protected RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }


    @Override
    public void onViewAttachedToWindow(@NonNull FeedViewHolder holder) {
        Log.d("IFCMVideoCard","onViewAttachedToWindow   holder.card="+holder.card+"   position="+holder.card.getPosition());
        mAttachedViewHolders.put(holder.hashCode(), new WeakReference<>(holder));
        super.onViewAttachedToWindow(holder);
    }

    protected RecyclerViewPageFeed.OnPageChangedListener mOnPageChangedListener;

    public RecyclerViewPageFeed.OnPageChangedListener getOnPageChangedListener() {
        if (mOnPageChangedListener == null) {
            mOnPageChangedListener = new RecyclerViewPageFeed.OnPageChangedListenerAdapter() {

                @Override
                public void onPageSelected(int lastPosition, int position) {
                    Log.d("IFCMVideoCard","RecyclerViewPageFeed onViewAttachedToWindow   lastPosition="+lastPosition+"   position="+position);

                    if (lastPosition != position) {
                        BasePagerCard lastCard = null;
                        BasePagerCard currentCard = null;
                        for (int i = 0; i < mAttachedViewHolders.size(); i++) {
                            int key = mAttachedViewHolders.keyAt(i);
                            WeakReference<FeedViewHolder> ref = mAttachedViewHolders.get(key);
                            if (ref != null && ref.get() != null
                                    && (ref.get().card instanceof BasePagerCard)) {
                                if (ref.get().card.getAbsoluteAdapterPosition() == lastPosition) {
                                    lastCard = (BasePagerCard) ref.get().card;
                                } else if (ref.get().card.getAbsoluteAdapterPosition() == position) {
                                    currentCard = (BasePagerCard) ref.get().card;
                                }
                            }
                        }
                        if (lastCard != null) {
                            lastCard.pageUnselected();
                        }
                        if (currentCard != null) {
                            currentCard.pageSelected();
                        }
                    }
                }

            };
        }
        return mOnPageChangedListener;
    }


}